package com.example.event_resource_java.authority.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.event_resource_java.commons.util.ApiResult;
import com.example.event_resource_java.trainapi.dao.JzUserPermissionDO;
import com.example.event_resource_java.trainapi.dao.TrainingResourcesDO;
import com.example.event_resource_java.trainapi.service.JzUserPermissionService;
import com.example.event_resource_java.trainapi.service.TrainingResourcesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;


/**
 * @author mengzhichao
 * @create 2025-09-02-9:02
 */
@RestController
@RequestMapping("/yrzh")
public class yrzhController {


    @Autowired
    private TrainingResourcesService trainingResourcesService;


    @Autowired
    private JzUserPermissionService jzUserPermissionService;


    /**
     * @auther: ak
     * @Description grade跟据等級查詢資源視頻  1初級    2中級     3高級
     * @param:  type 0文档，1视频，2课件（ppt），3动画，4图片，5链接
     * @return:
     * @date: 2024/4/22 9:55
     */
    @GetMapping("/list")
    public ApiResult list(String grade,String type,@RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize) {

        Page<TrainingResourcesDO> page = new Page<>(pageNum,pageSize);

        QueryWrapper<TrainingResourcesDO> wrapper =new QueryWrapper();

        if (grade!=null&&!grade.equals("")) {
            String coursesId = "1320972346629394433"; //默认查幼儿照护初级

            if (grade.equals("2")) {
                coursesId = "1320972579358740481"; //幼儿照护中级
            }

            if (grade.equals("3")) {
                coursesId = "1697420183621373953"; //幼儿照护高级
            }
            wrapper.eq("courses_id",coursesId);
        }

        if (type!=null&&!type.equals("")){
            if ("1".equals(type) || "5".equals(type)) {
                // type =1 和 type =5 都属于视频，查询这两种类型
                wrapper.in("type", Arrays.asList("1", "5"));
            } else {
                // 其他类型按原样处理
                wrapper.eq("type", type);
            }
        }

        IPage<TrainingResourcesDO> trainingResourcesDOIPage = trainingResourcesService.page(page, wrapper);
        return ApiResult.success(trainingResourcesDOIPage);
    }








    /**
     * @auther: ak
     * @return: 权限查询
     * @date: 2024/4/22 9:55
     *
     *  1老师，2学生
     *
     */
    @GetMapping("/powerSel")
    public ApiResult powerSel(String grade,String userId,String role) {
        Boolean rst = false; //默认未开通权限

        String coursesId = "1320972346629394433"; //默认查幼儿照护初级

        if (grade.equals("2")) {
            coursesId = "1320972579358740481"; //幼儿照护中级
        }

        if (grade.equals("3")) {
            coursesId = "1697420183621373953"; //幼儿照护高级
        }


        if (role.equals("1")){  //如果是老师权限
            QueryWrapper<JzUserPermissionDO> wrapper =new QueryWrapper();
            wrapper.eq("user_id",userId);
            wrapper.eq("courses_id",coursesId);
            List<JzUserPermissionDO> list = jzUserPermissionService.list(wrapper);
            if (list.size()>0){
                rst=true;//有该等级权限
            }
        }else {
            Long teacherId = jzUserPermissionService.getTeacherId(Long.valueOf(userId));
            QueryWrapper<JzUserPermissionDO> wrapper =new QueryWrapper();
            wrapper.eq("user_id",teacherId);
            wrapper.eq("courses_id",coursesId);
            List<JzUserPermissionDO> list = jzUserPermissionService.list(wrapper);
            if (list.size()>0){
                rst=true;//有该等级权限
            }
        }
        return ApiResult.success(rst);
    }


    /**
     * 增加播放次数
     * @return
     */
    @GetMapping("/updateResourcesViewCount")
    public ApiResult updateResourcesViewCount(Long id) {
        TrainingResourcesDO trainingResourcesDO = trainingResourcesService.getById(id);
        if (trainingResourcesDO!=null){
            trainingResourcesDO.setViewCount(trainingResourcesDO.getViewCount() == null?1:trainingResourcesDO.getViewCount()+1);
        }
        trainingResourcesService.updateById(trainingResourcesDO);
        return ApiResult.success();
    }

}
